<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <customDeptTree @clickDeptNode="clickDeptNode" :show-search-input="true"/>
      </el-col>

      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
          <el-form-item label="手机号" prop="account">
            <el-input v-model="queryParams.account" placeholder="请输入手机号" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="网关MAC" prop="gatewayMac">
            <el-input v-model="queryParams.gatewayMac" placeholder="请输入网关MAC" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备名称或MAC" prop="deviceName">
            <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称或MAC" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="被动类型" prop="type">
            <el-select v-model="queryParams.endType" placeholder="被动类型"
                       clearable
                       size="small"
            >
              <el-option
                v-for="dict in statusOptionsType2"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="模式" prop="type">
            <el-select v-model="queryParams.mode" placeholder="模式"
                       clearable
                       size="small"
            >
              <el-option
                v-for="dict in statusOptionsType1"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button class="filter-item" type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
            >搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                       v-hasPermi="['system:detail:linkage:export']"
            >导出
            </el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10">
        </el-row>
        <el-col>
          <el-table v-loading="loading" :data="relationList" border>
            <el-table-column label="手机号" align="center" prop="account" width="120"/>
            <el-table-column label="家庭名称" align="center" prop="homeName" :show-overflow-tooltip="true"/>
            <el-table-column label="网关Mac" align="center" prop="gatewayMac" :show-overflow-tooltip="true"/>
            <el-table-column label="主动设备" align="center" prop="startDeviceName" :show-overflow-tooltip="true">
              <template slot-scope="scope">

                <template v-if="scope.row.startDeviceName">
                  <el-popover trigger="hover" placement="bottom-start">
                    <el-descriptions :column=1 style="width: 250px" :labelStyle="{'font-weight': 'bold'}">
                      <el-descriptions-item label="设备名称">{{ scope.row.startDeviceName }}</el-descriptions-item>
                      <el-descriptions-item label="设备名MAC">{{ scope.row.startDeviceMac }}</el-descriptions-item>
                      <el-descriptions-item label="设备类型">{{ scope.row.startDeviceType }}</el-descriptions-item>
                      <el-descriptions-item label="类型名称">{{ scope.row.startCategoryName }}</el-descriptions-item>
                    </el-descriptions>
                    <span slot="reference">{{ scope.row.startDeviceName }}</span>
                  </el-popover>
                </template>
                <template v-else>
                  <el-tag type="info">未知</el-tag>
                </template>
              </template>
            </el-table-column>
            <!--            <el-table-column label="从设备ID" align="center" prop="endDeviceId"/>-->
            <!--            <el-table-column label="主动设备MAC" align="center" prop="startDeviceMac" :show-overflow-tooltip="true"/>-->
            <!--            <el-table-column label="开关状态" align="center" prop="startSwitchState" width="80">-->
            <!--              <template slot-scope="scope">-->
            <!--                <template v-if="scope.row.startSwitchState==='OPEN'">-->
            <!--                  <div class="myTagOpen" style="margin-bottom: 2px">开启</div>-->
            <!--                </template>-->
            <!--                <template v-else>-->
            <!--                  <div class="myTagClose">关闭</div>-->
            <!--                </template>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <el-table-column label="被动设备" align="center" prop="endDeviceName" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <template v-if="scope.row.endType==='device'">
                  <template v-if="scope.row.endDeviceName">
                    <el-popover trigger="hover" placement="bottom-start">
                      <el-descriptions :column=1 style="width: 250px" :labelStyle="{'font-weight': 'bold'}">
                        <el-descriptions-item label="设备名称">{{ scope.row.endDeviceName }}</el-descriptions-item>
                        <el-descriptions-item label="设备名MAC">{{ scope.row.endDeviceMac }}</el-descriptions-item>
                        <el-descriptions-item label="设备类型">{{ scope.row.endDeviceType }}</el-descriptions-item>
                        <el-descriptions-item label="类型名称">{{ scope.row.endCategoryName }}</el-descriptions-item>
                      </el-descriptions>
                      <span slot="reference">{{ scope.row.endDeviceName }}</span>
                    </el-popover>
                  </template>
                  <template v-else>
                    <el-tag type="info">未知</el-tag>
                  </template>
                </template>
                <template v-else-if="scope.row.endType==='group'">
                  <template v-if="scope.row.endGroupName">
                    <div> {{ scope.row.endGroupName }}</div>
                  </template>
                  <template v-else>
                    <el-tag type="info">未知</el-tag>
                  </template>

                </template>
                <template v-else>
                  <el-tag type="info">未知</el-tag>
                </template>
              </template>
            </el-table-column>
            <!--            <el-table-column label="关联MAC" align="center" prop="endDeviceMac"/>-->
            <!--            <el-table-column label="关联群组" align="center" prop="endGroupName"/>-->
            <el-table-column label="被动类型" align="center" prop="endType" width="100">
              <template slot-scope="scope">
                <template v-if="scope.row.endType==='device'">
                  <div>
                    <el-tag effect="dark">设备</el-tag>
                  </div>
                </template>
                <template v-else-if="scope.row.endType==='group'">
                  <div>
                    <el-tag type="success" effect="dark">群组</el-tag>
                  </div>
                </template>
              </template>
            </el-table-column>
            <el-table-column label="模式" align="center" prop="mode" :show-overflow-tooltip="true" width="100">
              <template slot-scope="scope">
                <template v-if="scope.row.mode==='customize'">
                  <div>
                    <el-tag effect="dark">自定义</el-tag>
                  </div>
                </template>
                <template v-else-if="scope.row.mode==='sync'">
                  <div>
                    <el-tag type="success" effect="dark">同步</el-tag>
                  </div>
                </template>
                <template v-else>
                  <div>
                    <el-tag type="warning" effect="dark">切换</el-tag>
                  </div>
                </template>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width"
                             :show-overflow-tooltip="true" width="100"
            >
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetailsView(scope.row)"
                           v-hasPermi="['system:detail:linkage:detail']"
                >详情
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                      :limit.sync="queryParams.pageSize" @pagination="getList"
          />
        </el-col>
      </el-col>
    </el-row>

    <!-- 详情极享设备联动配置对话框 -->
    <!-- 详情多联多控对话框 -->
    <el-dialog :title="title" :visible.sync="details" width="1000px" append-to-body>
      <template slot-scope="scope">
        <el-descriptions title="主动设备信息" :column=3 contentClassName="my-content" labelClassName="my-label">

        </el-descriptions>
        <span v-if="!myScope.startDeviceName"><el-tag type="info">设备信息为空</el-tag></span>
        <el-descriptions v-else :column=3 contentClassName="my-content" labelClassName="my-label">
          <el-descriptions-item label="设备名称">{{ form1.deviceName }}</el-descriptions-item>
          <el-descriptions-item label="MAC地址">{{ form1.deviceMac }}</el-descriptions-item>
          <el-descriptions-item label="设备类型">
            <template>
              {{ form1.deviceType }}
            </template>
            <template>
              <el-tag type="success"> {{ form1.defaultDeviceName }}</el-tag>
            </template>
          </el-descriptions-item>
        </el-descriptions>


        <el-divider/>

        <!--    以下是第二部分    /-->


        <!--        设备-->
        <template v-if="myScope.endType==='device'">
          <el-descriptions title="被动设备信息" :column=3 contentClassName="my-content" labelClassName="my-label">

          </el-descriptions>
          <span v-if="!myScope.startDeviceName"><el-tag type="info">设备信息为空</el-tag></span>
          <el-descriptions v-else :column=3 contentClassName="my-content" labelClassName="my-label">
            <el-descriptions-item label="设备名称">{{ form2.deviceName }}</el-descriptions-item>
            <el-descriptions-item label="MAC地址">{{ form2.deviceMac }}</el-descriptions-item>
            <el-descriptions-item label="设备类型">
              <template>
                {{ form2.deviceType }}
              </template>
              <template>
                <el-tag type="success"> {{ form2.defaultDeviceName }}</el-tag>
              </template>
            </el-descriptions-item>
            <el-descriptions-item label="联动动作">
              <template v-if="actions">
                <div v-if="actions.type==='light'||actions.type==='rgbw'||actions.type==='group'">
                  <el-tag effect="dark">
                    <span v-if="actions.duration!==undefined">{{ actions.duration }}挡 |</span>
                    <span v-if="actions.setBrightness!==undefined">亮度{{ actions.setBrightness }}% |</span>
                    <span v-if="actions.setColorTemperature!==undefined">色温{{ actions.setColorTemperature }}% |</span>
                    <span v-if="actions.state!==undefined">{{ actions.state === 'OPEN' ? '开' : '关' }}</span>
                  </el-tag>
                </div>
                <div v-else-if="actions.type==='curtain'">
                  <el-tag effect="dark">
                    <span v-if="actions.percentage!==undefined">开合度：{{ actions.percentage }} |</span>
                    <span v-if="actions.state!==undefined">{{ actions.state === 'OPEN' ? '开' : '关' }}</span>
                  </el-tag>
                </div>
              </template>
              <template v-else>
                <template v-if="myScope.mode==='sync'">
                  <el-tag type="success" effect="dark">同步</el-tag>
                </template>
                <template v-else-if="myScope.mode==='switch'">
                  <el-tag type="warning" effect="dark">切换</el-tag>
                </template>
                <template v-else>
                  <el-tag type="info">未知</el-tag>
                </template>
              </template>

            </el-descriptions-item>
          </el-descriptions>
        </template>


        <!--        群组-->
        <template v-else-if="myScope.endType==='group'">
          <el-descriptions title="关联群组信息" :column=3 contentClassName="my-content" :show-overflow-tooltip="true">
          </el-descriptions>
          <span v-if="!myScope.endGroupName"><el-tag type="info">设备信息为空</el-tag></span>
          <el-descriptions v-else :column=3 contentClassName="my-content" labelClassName="my-label">
            <el-descriptions-item label="群组名称">{{ myScope.endGroupName }}</el-descriptions-item>
            <el-descriptions-item label="群组类型">
              <template v-for="dict in statusOptionsType">
                <template v-if="dict.dictValue===form3.type">
                  <el-tag style="margin-top: -15px">{{ dict.dictLabel }}</el-tag>
                </template>
              </template>
            </el-descriptions-item>
            <el-descriptions-item label="联动动作">
              <template v-if="actions">
                <div v-if="actions.type=='light'||actions.type=='rgbw'||actions.type=='group'">
                  <el-tag effect="dark">
                    <span v-if="actions.duration!=undefined">{{ actions.duration }}挡 |</span>
                    <span v-if="actions.setBrightness!=undefined">亮度{{ actions.setBrightness }}% |</span>
                    <span v-if="actions.setColorTemperature!=undefined">色温{{ actions.setColorTemperature }}% |</span>
                    <span v-if="actions.state!=undefined">{{ actions.state == 'OPEN' ? '开' : '关' }}</span>
                  </el-tag>
                </div>
                <div v-else-if="actions.type=='curtain'">
                  <el-tag effect="dark">
                    <span v-if="actions.percentage!=undefined">开合度：{{ actions.percentage }} |</span>
                    <span v-if="actions.state!=undefined">{{ actions.state == 'OPEN' ? '开' : '关' }}</span>
                  </el-tag>
                </div>
              </template>
              <template v-else>
                <template v-if="myScope.mode==='sync'">
                  <el-tag type="success" effect="dark">同步</el-tag>
                </template>
                <template v-else-if="myScope.mode==='switch'">
                  <el-tag type="warning" effect="dark">切换</el-tag>
                </template>
                <template v-else>
                  <el-tag type="info">未知</el-tag>
                </template>
              </template>
            </el-descriptions-item>
          </el-descriptions>
          <el-descriptions title="关联设备"></el-descriptions>
          <template>
            <el-table v-loading="loading" :data="relationDeviceData">
              <el-table-column prop="deviceName" label="设备名称" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <span v-if="!scope.row.deviceName"><el-tag type="info">设备不存在</el-tag></span>
                  <span v-else>{{ scope.row.deviceName }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="deviceMac" label="设备MAC" width="180">
              </el-table-column>
              <el-table-column prop="deviceType" label="设备类型" width="150">
                <template slot-scope="scope">
                  <span>{{ scope.row.deviceType }}</span>
                </template>
              </el-table-column>

              <el-table-column label="类型名称" width="200">
                <template slot-scope="scope">
                  <div class="myTagCategoryName" v-if="scope.row.defaultDeviceName">
                    {{ scope.row.defaultDeviceName }}
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </template>
      </template>


      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { exportRelation, listRelation } from '@/api/system/linkRelation'

import { getGroup } from '@/api/system/group'
import { fetchStatus, getDevice } from '@/api/system/device'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'Relation',
  data() {
    return {
      statusOptionsType1: [{ dictValue: 'switch', dictLabel: '切换' }, {
        dictValue: 'sync',
        dictLabel: '同步'
      }, { dictValue: 'customize', dictLabel: '自定义' }],
      statusOptionsType2: [{ dictValue: 'device', dictLabel: '设备' }, {
        dictValue: 'group',
        dictLabel: '群组'
      }],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      statusOptionsType: [{ dictValue: 'light', dictLabel: '灯光类' }, {
        dictValue: 'curtain',
        dictLabel: '窗帘类'
      }, { dictValue: 'rgbw', dictLabel: '多彩灯类' }],
      myScope: undefined,
      // 极享设备联动配置表格数据
      relationList: [],
      // 极享设备联动配置树选项
      relationOptions: [],
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      relationDeviceData: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      details: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gatewayId: undefined,
        startDeviceId: undefined,
        endId: undefined,
        startDeviceMac: undefined,
        startOdIndex: undefined,
        startDeviceType: undefined,
        startProductType: undefined,
        startSwitchState: undefined,
        endDeviceMac: undefined,
        endDeviceType: undefined,
        endProductType: undefined,
        endOdIndex: undefined,
        endGroupId: undefined,
        endType: undefined,
        actions: undefined,
        mode: undefined,
        modifyTime: undefined,
        deviceName: undefined
      },
      // 表单参数
      form1: {
        actions: undefined
      },
      form2: {},
      form3: {},
      actions: {},
      fetchStatusHtml1: '',
      fetchStatusHtml2: ''
    }

  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询极享设备联动配置列表 */
    getList() {
      this.loading = true
      listRelation(this.queryParams).then(response => {
        this.relationList = response.rows
        this.total = response.total
        this.loading = false
      })

    },
    // 取消按钮
    cancel() {
      this.open = false
      this.details = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        gatewayId: undefined,
        startDeviceId: undefined,
        endId: undefined,
        startDeviceMac: undefined,
        startOdIndex: undefined,
        startDeviceType: undefined,
        startProductType: undefined,
        startSwitchState: undefined,
        endDeviceMac: undefined,
        endDeviceType: undefined,
        endProductType: undefined,
        endOdIndex: undefined,
        endGroupId: undefined,
        endType: undefined,
        actions: undefined,
        mode: undefined,
        createTime: undefined,
        modifyTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 详情按钮操作 */
    handleDetailsView(row) {
      this.reset()
      this.form1 = undefined
      this.form2 = undefined
      this.form3 = undefined
      this.myScope = undefined
      this.fetchStatusHtml1 = ''
      this.fetchStatusHtml2 = ''
      this.relationDeviceData = undefined

      const id1 = row.startDeviceId
      const id2 = row.endId
      const id3 = row.endGroupId
      this.myScope = JSON.parse(JSON.stringify(row))
      this.actions = JSON.parse(row.actions)
      if (id1 !== undefined && id1 !== '' && id1 != null) {
        getDevice(id1).then(response => {
          this.form1 = response.data
          this.details = true
          this.title = '联控详情'
          if (this.form1) {
            this.fetchStatusHtml1 = fetchStatus(this.form1.actions, this.form1.deviceType)
          } else {
            this.fetchStatusHtml1 = ''
          }

        })
      }
      if (row.endType === 'device') {
        getDevice(id2).then(response => {
          this.form2 = response.data
          this.details = true
          this.title = '联控详情'
          if (this.form2) {
            this.fetchStatusHtml2 = fetchStatus(this.form2.actions, this.form2.deviceType)
          } else {
            this.fetchStatusHtml2 = ''
          }
        })
      }
      if (row.endType === 'group') {
        getGroup(id3).then(response => {
          this.form3 = response.data
          this.relationDeviceData = response.data.deviceList
          this.open = true
          this.title = '联控详情'
          console.log(this.form3)
        })
      }

    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出数据项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportRelation(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    },

    /**
     * 点击树节点
     * @param value
     */
    clickDeptNode(value) {
      this.queryParams.deptId = value.id
      this.getList()
    }
  }
}
</script>
